//index.js
//获取应用实例
const app = getApp()
import { request } from "../../utils/request.js";
import { wushiyin } from "../../utils/wushiyin.js";
Page({
  data: {
    left: 0,
    current: 0,
    current_yin_q: [],
    current_yin_z: [],
    current_yin_a: [],
    state: 0,
    isHidden: true,
    animateData: {},
    pronunciation: '',
    bishunPics: {}
  },
  wushiyin: wushiyin,
  canPlay: true,
  //监听轮播图切换 
  content: "",
  swiper_change: function (e) {
    const current = e.detail.current;
    const left = current * 33;
    this.setData({
      left,
      current
    })
  },
  top_tab_tap: function (e) {
    const index = e.currentTarget.dataset.index;
    let left = index * 33;
    this.setData({
      current: index,
      left
    })
  },
  change_tap: function (e) {
    const {
      state
    } = e.currentTarget.dataset;
    if (state == this.data.state) {
      return
    }
    const { arr_q: q, arr_z: z, arr_a: a } = this.change(state);
    this.setData({
      current_yin_q: q,
      current_yin_z: z,
      current_yin_a: a,
      state
    })
  },
  change: function (state) {
    const wushiyin = this.wushiyin;
    var arr_q, arr_z, arr_a;
    switch (state) {
      case 0:
        arr_q = wushiyin.q.ping;
        arr_z = wushiyin.z.ping;
        arr_a = wushiyin.a.ping;
        break;
      case 1:
        arr_q = wushiyin.q.pian;
        arr_z = wushiyin.z.pian;
        arr_a = wushiyin.a.pian;
    }
    return { arr_q, arr_z, arr_a }
  },
  speak: function (e) {
    const { text, special } = e.currentTarget.dataset;
    if (special) {
      // 没有真实显示的内容不需要该功能
      return
    }
    this.play(text);
  },
  card_speak: function () {

    this.play(this.content);

  },
  actx: null,
  play(t) {
    if (this.actx != null) {
      this.actx.destroy()
    }
    var audioCtx = wx.createInnerAudioContext();

    this.actx = audioCtx;
    let text = encodeURI(t);
    audioCtx.src = `https://m.fanyi.qq.com/ttsfile?language=2&text=${text}&platform=H5`;
    audioCtx.play()
    audioCtx.onPlay(() => {
      // console.log('开始播放')
    })
    audioCtx.onError((err) => {
      console.log(err)
      wx.showToast({
        title: '读音走丢了TAT',  //标题  
        mask: false,  //是否显示透明蒙层，防止触摸穿透，默认：false  
      })
    })
    // if (this.canPlay) {
    // this.canPlay = false;
    //   audioCtx.onEnded(() => {
    //     this.canPlay = true
    //   })
    // } else {
    //   wx.showToast({
    //     title: '太快了~',
    //     icon:"none"
    //   })
    //   return
    // }
  },
  cardShow(e) {
    const { special, text, pronunciation } = e.currentTarget.dataset;
    if (special) {
      // 没有真实显示的内容不需要该功能
      return
    }
    var p = pronunciation
    request({
      url: "http://127.0.0.1:3000/bishunPics",
      data: {
        p
      }
    }).then((res) => {
      this.setData({
        bishunPics: res.data
      })
      wx.hideLoading()
    })
    this.content = text;
    let animateData = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0
    })
    animateData.translateZ(0).opacity(1).step();
    this.setData({
      isHidden: false,
      animateData: animateData.export(),
      pronunciation,
      state: this.data.state
    })

  },
  cardHidden() {
    let animateData = wx.createAnimation({})
    animateData.translateZ(-5000).opacity(0.1).step();
    this.setData({
      isHidden: true,
      animateData: animateData.export()
    })
  },
  onLoad: function () {
    const { arr_q: q, arr_z: z, arr_a: a } = this.change(0);
    this.setData({
      current_yin_q: q,
      current_yin_z: z,
      current_yin_a: a
    })
  },
})